/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmc.rra.service.impl;

import com.cmc.rra.service.TaxTypeDetailsService;
import com.cmc.rra.sub.TaxTypeDetails;
import com.cmc.rra.utils.HibernateUtils;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author cmc
 */
public class TaxTypeDetailsServiceImpl implements TaxTypeDetailsService{

    @Override
    public void create(TaxTypeDetails taxTypeDetails) {
        Session session = HibernateUtils.currentSession();
        Transaction tx = null;
        boolean rollback = true;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(taxTypeDetails);
            tx.commit();
            rollback = false;
        } catch (Exception exception) {
            exception.printStackTrace();

        } finally {
            if (rollback && tx != null) {
                tx.rollback();
            }
            HibernateUtils.closeSession();
        }
    }

    @Override
    public List<TaxTypeDetails> getAll() {
        Session session = HibernateUtils.currentSession();
        List<TaxTypeDetails> taxTypeDetailsList = null;
        try{
            Query query = session.createQuery("from TaxTypeDetails");
            taxTypeDetailsList = query.list();
        }catch(Exception exception){
            exception.printStackTrace();
        }finally{
            HibernateUtils.closeSession();
        }
        
        return taxTypeDetailsList;
    }

    @Override
    public TaxTypeDetails getByTaxTypeNumber(Long taxTypeNumber) {
        Session session = HibernateUtils.currentSession();
        TaxTypeDetails taxTypeDetails = null;
        try{
            Query query = session.createQuery("from TaxTypeDetails details where details=:taxTypeNumber");
            query.setLong("taxTypeNumber", taxTypeNumber);
            taxTypeDetails = (TaxTypeDetails) query.uniqueResult();
            
        }catch(Exception exception){
            exception.printStackTrace();
        }finally{
            HibernateUtils.closeSession();
        }
        return taxTypeDetails;
    }
    
    
    
}
